package com.kyzh.core.adapters

import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.util.setHeight
import com.gushenge.atools.util.setWidth
import com.kyzh.core.R
import com.kyzh.core.utils.SampleCoverVideo
import com.kyzh.core.utils.ViewUtils.dp
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.setVisibility
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView

class GameDetailBannerAdapter(layout:Int,var bean:ArrayList<String>?,var video: String?,var aotoBoFang :Boolean= false):BaseQuickAdapter<String,
        BaseViewHolder>
    (layout,
    bean) {
    override fun convert(helper: BaseViewHolder, item: String) {
        val videoPlayer = helper.getView<SampleCoverVideo>(R.id.videoPlayer)
        val view = helper.getView<ImageView>(R.id.image)
        if(!video.isNullOrEmpty()&& helper.position==0){
            view.setVisibility(false)
            videoPlayer.setVisibility(true)
            videoPlayer.apply {

                isNeedShowWifiTip = false
                isLooping = true
                dismissControlTime = 2000
                setIsTouchWiget(true)
                setThumbPlay(true)
                backButton.visibility = View.GONE
                backButton.setOnClickListener {
                    GSYVideoManager.backFromWindowFull(context)
                }
                fullscreenButton.setOnClickListener {
                    backButton.visibility = View.VISIBLE
                    startWindowFullscreen(context,false,false)
                }
                startButton.setOnClickListener {
                    when (currentState) {
                        GSYVideoView.CURRENT_STATE_PAUSE -> {
                            onVideoResume(false)
                        }
                        GSYVideoView.CURRENT_STATE_PLAYING -> {
                            onVideoPause()
                        }
                        else -> {
                            startPlayLogic()
                        }
                    }
                }
                isLockLand = true;
                isAutoFullWithSize = false
                isShowFullAnimation = true;
                fullscreenButton.setOnClickListener {
                    orientationUtils?.resolveByClick();
                    //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                    videoPlayer.startWindowFullscreen(context, true, true);
                }
                setVideoAllCallBack(object : GSYSampleCallBack() {
                    override fun onPrepared(url: String?, vararg objects: Any?) {
                        super.onPrepared(url, *objects)
                        val full: Boolean =
                            videoPlayer.isIfCurrentIsFullscreen()
                        if (!videoPlayer
                                .isIfCurrentIsFullscreen()
                        ) {
                        }
                        if (videoPlayer.isIfCurrentIsFullscreen()) {
                            GSYVideoManager.instance().setLastListener(videoPlayer)
                        }
                    }
                    override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
                        super.onEnterFullscreen(url, *objects)
                    }
                    override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                        super.onQuitFullscreen(url, *objects)
                    }
                })
            }
            videoPlayer.setUp(video, true,  "")
            if(aotoBoFang){
                videoPlayer. startPlayLogic()
            }else{
                videoPlayer.onVideoPause()
            }
        }else{
            if(!aotoBoFang){
                if(bean?.size==1){
                    view.setHeight(175.dp)
                    view.setWidth(306.dp)
                }else{
                    view.setWidth(139.dp)
                    view.setHeight(95.dp)
                }
            }

            videoPlayer.setVisibility(false)
            view.setVisibility(true)
            if(!item.contains(" ")){
                view.loadImage(item)
            }

        }
    }
}

var orientationUtils: OrientationUtils? = null
private fun initOrientationUtils(context: Context, standardGSYVideoPlayer: StandardGSYVideoPlayer, full: Boolean) {
    orientationUtils = OrientationUtils(context as Activity?, standardGSYVideoPlayer)
    //是否需要跟随系统旋转设置
    orientationUtils?.apply {
        setRotateWithSystem(false);
        setEnable(false)
        setIsLand(1)
    }
}